﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadingApp1
{
    /// <summary>
    /// Klasa watku producenta
    /// </summary>
    public class Producer
    {
        string name;
        Storehouse storehouse;
        int time;

        public Producer(Storehouse store, string name, int time)
        {
            this.name = name;
            this.time = time;
            this.storehouse = store;
        }

        public void Run()
        {
            while (true)
            {
                Console.WriteLine("Producer " + name + " starts making new product");
                Thread.Sleep(time);
                Console.WriteLine("Producer " + name + " try add new product to storehouse");
                bool added = false;
                do
                {
                    added = storehouse.AddProduct("Producer:" + name);

                    if (added == false)
                    {
                        Console.WriteLine("Producer " + name + "must wait to add new product");
                        Thread.Sleep(500);
                    }
                }
                while (!added);
            }
        }

    }
}
